﻿//===================================================================================
// Copyright (c) Ryan Garrett.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===================================================================================
using System;

namespace Caffeine.Core
{
    /// <summary>
    /// Contains a validation error's description and classification code
    /// </summary>
    public class ValidationError : IEquatable<ValidationError>
    {
        /// <summary>
        /// Gets or sets the validation error code. Error codes can be used to classify this
        /// instance as a warning, error, critical error, etc.
        /// </summary>
        public virtual Int32 ErrorCode
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the validation error description
        /// </summary>
        public virtual String ErrorDescription
        {
            get;
            set;
        }

        /// <summary>
        /// Gets a string representation of this error
        /// </summary>
        /// <returns>The ErrorDescription value</returns>
        public override String ToString()
        {
            return ErrorDescription;
        }

        /// <summary>
        /// Checks whether or not a ValidationError instance is considered the same as this instance.
        /// </summary>
        /// <param name="other">ValidationError to compare against</param>
        /// <returns>True if they are considered equal, otherwise false.</returns>
        public Boolean Equals(ValidationError other)
        {
            return other != null &&
                   ErrorCode == other.ErrorCode &&
                   ErrorDescription == other.ErrorDescription;
        }

        /// <summary>
        /// Checks whether or not a ValidationError instance is considered the same as this instance.
        /// </summary>
        /// <param name="other">ValidationError to compare against</param>
        /// <returns>True if they are considered equal, otherwise false.</returns>
        public override Boolean Equals(Object obj)
        {
            return Equals(obj as ValidationError);
        }

        /// <summary>
        /// Gets the hash code for this instance
        /// </summary>
        /// <returns>Hash code for this instance</returns>
        public override Int32 GetHashCode()
        {
            return ErrorCode.GetHashCode() ^ (ErrorDescription ?? String.Empty).GetHashCode();
        }
    }
}
